//
//  WorkingParticularsView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/11.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "WorkingParticularsView.h"
#import "SYRequestAPI+Teach.h"

@implementation WorkingParticularsView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andClassID:(NSString *)classID andePaperID:(NSString *)paperID andStudentID:(NSString *)studentID{
    if ([super initWithFrame:frame]) {
        self.classID = classID;
        self.paperId = paperID;
        self.studentId = studentID;
        [self initSubView];
        [self getZuoYeXiangQingRequest];
        //        [self getAnShiTiTongJiRequest];
    }
    return self;
}


- (void)initSubView{
    self.backgroundColor = [UIColor whiteColor];
    _heightArray = [NSMutableArray new];
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0).offset(-20);
    }];
//    [self getZuoYeXiangQingRequest];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0) style:UITableViewStyleGrouped];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[WPVTableViewCell class] forCellReuseIdentifier:@"WPVTableViewCell"];
        _headerView = [[SJXQHeaderView alloc]init];
        _headerView.frame = CGRectMake(0, 0, self.frame.size.width, 64);
        tableView.tableHeaderView = _headerView;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = [UIColor whiteColor];
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _model.dataQuestionArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    JiaoXueDataQuestion *m = _model.dataQuestionArray[indexPath.row];
    UILabel *label = [UILabel new];
    label.attributedText = [self attributedStringWithHTMLString:[NSString stringWithFormat:@"%@/n/n%@",m.questionContent,m.answerContent]];
    CGFloat height = [label.attributedText boundingRectWithSize:CGSizeMake(self.tableList.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;//针对富文本有专门的计算动态高度的方法，与nsstring 大同小异；self.lable.height = height;
//
//    UILabel *label2 = [UILabel new];
//    label2.attributedText = [self attributedStringWithHTMLString:[NSString stringWithFormat:@"%@",m.answerContent]];
//    CGFloat height2 = [label2.attributedText boundingRectWithSize:CGSizeMake(self.tableList.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
    return height+20;
}
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPVTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[WPVTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WPVTableViewCell"];
    }
    JiaoXueDataQuestion *m = _model.dataQuestionArray[indexPath.row];
    [cell.webView loadHTMLString:[NSString stringWithFormat:@"%@ %@",m.questionContent,m.answerContent] baseURL:nil];
    cell.webView.UIDelegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = CONTENTBACKCOLOR;
    return cell;
}
#pragma mark - 教学-作业-作业详情-请求
- (void)getZuoYeXiangQingRequest{
    NSDictionary *parama = @{@"classId":_classID,
                          @"paperId":_paperId,
                          @"studentId":@"1"};
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getHomeWordServiceHomeWorKDetail:parama success:^(id  _Nonnull responseObject) {
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        self->_model = [JiaoXueZuoYeXiangQingModel yy_modelWithJSON:dic];
        self->_model.dataQuestionArray = [NSMutableArray new];
        for (NSDictionary *dic in self->_model.dataQuestion) {
            JiaoXueDataQuestion *md = [JiaoXueDataQuestion yy_modelWithJSON:dic];
            [self->_model.dataQuestionArray addObject:md];
            md.dataQuestionAnswerArray = [NSMutableArray new];
            for (NSDictionary *dic in md.dataQuestionAnswer) {
                JiaoXueDataQuestionAnswer *mdd = [JiaoXueDataQuestionAnswer yy_modelWithJSON:dic];
                [md.dataQuestionAnswerArray addObject:mdd];
            }
        }
        NSLog(@"得到-教学-作业-作业详情 %@",dic);
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
}
#pragma mark - 设置头部视图的值

/**
 
 */
- (void)setValueForHeader{
    _headerView.titleLabel.text = [NSString stringWithFormat:@"%@ %@",_headerModel.className,_headerModel.lessonName];
    _headerView.mentorLabel.text = [NSString stringWithFormat:@"%@",_headerModel.paperName];
    _headerView.detailLabel.text = [NSString stringWithFormat:@"应交：%@ 实交：%@",_headerModel.totalStudents,_headerModel.handStudents];
    _headerView.titleLabel.text = _headerModel.createDate;
}
@end
